"""
File:forms.py
Author: anning
Date:-03-30
Connect:792910452@qq.com
Description:
"""
from flask_wtf import FlaskForm
from wtforms import StringField,SelectField,SubmitField
from wtforms.validators import DataRequired
from app.models import Category


class AddTodoForm(FlaskForm):
    content=StringField(
        label='任务内容',
        validators=[DataRequired()],
        render_kw={
            'class':'form-control',
            'placeholder':'添加任务信息'
        }
        # 当使用宏时，租用类名为form-control的模板，在list.html中
    )
    # 下拉表
    category=SelectField(
        label='任务类型',
        coerce=int ,
        # choice=[(item.id,item.name) for item in Category.query.all()]
        render_kw={
            'class':'btn btn-choice dropdown-toggle',
            'type': "button",
            'data-toggle': "dropdown",
            'aria-haspopup': "true",
            'aria-expanded': "false"
        }
    )
    submit=SubmitField(
        label='添加任务',
        render_kw={
            'class':'btn btn-default btn-todo-add'
        }
    )

    # 构造方法在实例化对象时自动执行，这里在每次实例化表单对象时，都会重新查询所有的分类，并展示在下拉表中
    def __init__(self):
        # 执行父类的构造方法
        super(AddTodoForm, self).__init__()
        categories=Category.query.all()
        if categories:
            self.category.choices=[(item.id,item.name) for item in categories]
        else:
            self.category.choices=[(-1,'请先创建分类')]


class EditTodoForm(FlaskForm):
    content=StringField(
        label='任务内容',
        validators=[DataRequired()]
    )
    category=SelectField(
        label='任务类型',
        coerce=int,
    )
    submit=SubmitField(
        label='编辑任务',
    )

    def __init__(self):
        super(EditTodoForm, self).__init__()
        categories=Category.query.all()
        if categories:
            self.category.choices=[(item.id,item.name) for item in categories]
        else:
            self.category.choices=[-1,'请先创建分类']